home *** CD-ROM | disk | FTP | other *** search
- Path: ccshst05.cs.uoguelph.ca!ccshst01!thay
- From: thay@uoguelph.ca (Toby K Hay)
- Newsgroups: comp.lang.c
- Subject: Re: I need random number function that returns a number between x and i ?
- Date: 6 Apr 1996 22:01:51 GMT
- Organization: University of Guelph
- Message-ID: <4k6pkf$3qj@ccshst05.cs.uoguelph.ca>
- References: <3166C942.2D8@ccis.com>
- NNTP-Posting-Host: ccshst01.cs.uoguelph.ca
- X-Newsreader: TIN [version 1.2 PL2]
-
- Bill Lund (wlund@ccis.com) wrote:
- : I need random number function that returns a number between x and i ?
- : Something like :
- : int randomer(int x , int i).
- : And returns a random number between x and i.
-
- Try:
- int Randomer(int x, int i)
- {
- return (x + rand() / ((RAND_MAX / (1 + i - x) + 1));
- }
-
- It should return a random integer in the interval [x, i] unless I've made
- a silly mistake. You might want to include a test of whether i is in
- fact greater than x to avoid errors such as divide by 0.
- Toby Hay thay@uoguelph.ca
-